package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.QuestionDao;
import cn.group7.padserver.entity.Page;
import cn.group7.padserver.pojo.Question;
import cn.group7.padserver.service.QuestionService;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谷汉斌
 * @description 问答服务实现类
 * @createTime 2024/8/3 下午4:00
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionDao questionDao;

    /**
     * 分页展示所有问答
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 结果
     */
    @Override
    public Page<Question> findPage(Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<Question> page = new Page<>();
        page.setData(questionDao.queryQuestionByPage(map));
        page.setPageNum(pageNum);
        page.setTotal(questionDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }

    /**
     * 根据关键字分页展示问答
     *
     * @param keys     关键字
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return 结果
     */
    @Override
    public Page<Question> findPageByKeys(String keys, Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("keys", keys);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<Question> page = new Page<>();
        page.setData(questionDao.selectByKeys(map));
        page.setPageNum(pageNum);
        page.setTotal(questionDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }

    /**
     * 根据id查询问答
     *
     * @param id id
     * @return 问答信息
     */
    @Override
    public Question selectById(Integer id) {
        return questionDao.selectQuestionById(id);
    }

    /**
     * 添加问答
     *
     * @param question 问答信息
     * @return 是否添加成功
     */
    @Override
    public boolean addQuestion(Question question) {
        //检查数据
        if (StrUtil.isBlank(question.getTitle()) || StrUtil.isBlank(question.getExpertName()) ||
                StrUtil.isBlank(question.getQuestioner()) || StrUtil.isBlank(question.getPhone()) ||
                StrUtil.isBlank(question.getPlantName())) {
            return false;
        }
        return questionDao.addQuestion(question) == 1;
    }

    /**
     * 根据id修改问答
     *
     * @param question 问答信息
     * @return 是否修改成功
     */
    @Override
    public boolean updateById(Question question) {
        return questionDao.updateById(question) == 1;
    }

    /**
     * 根据id删除问答
     *
     * @param id id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return questionDao.deleteById(id) == 1;
    }

    /**
     * 根据类型查询问答
     *
     * @param kind 类型
     * @return 问答信息
     */
    @Override
    public List<Question> selectQuestionByKind(String kind) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Question question = new Question();
        if ("questioner".equals(kind)) {
            question.setQuestioner(name);
        } else {
            question.setExpertName(name);
        }
        return questionDao.selectByQuestion(question);
    }
}
